<?php
/**
 * 系統名稱: Lotto 系統
 * 檔案說明: 專案管理-修改
 * $Author$
 * $Id$
 *
 */
//exit();
$strWeb = $_SERVER[argv][1];
$_SERVER["DOCUMENT_ROOT"] = "/home/share/www/".$strWeb."/secure";
$strError = $strWeb."  (".date("Y-m-d H:i:s").")\n";
$strError .= "===========================================\n";

$_isDBExist = "no";
$_DBSite = "Master";
if(!file_exists($_SERVER["DOCUMENT_ROOT"]."/config.inc.php"))exit($strError." no config.inc.php");
include("/home/share/www/SourceSite-utf8/secure/config.inc.php");
include(__Root_Path."/config.inc.php");
include(__DBConnect_File);

if($_strLogErr != ""){
	@mysql_close($$_Conn);
	exit($strError.$strLogError.$_strLogErr);
}
if(!preg_match("/utf8/i", $_blDBName))exit($strError." no UTF8 DB");

/*
會員  
信用 1000
下注額度 2000
下注日期 2009-12-08 以後

股東
信用 100000
下注額度 30000
下注日期 2009-12-15 以後

總代理
信用 100000
下注額度 30000
下注日期 2009-12-15 以後

代理商
信用 10000
下注額度 5000
下注日期 2009-12-15 以後
*/
$strPayupDate = date("Y-m-d", mktime(0, 0, 0, date("m"), date("d") - 28, date("Y")));//'2009-12-15';
$strError .= "PayupDate => ".$strPayupDate."\n\n";

$arrCondition = array();
$arrCondition[] = array('UID' => 'MemID', 'OverCredit' => 1000, 'OverStake' => 2000, 'Group' => 1);
$arrCondition[] = array('UID' => 'SubID', 'OverCredit' => 10000, 'OverStake' => 5000, 'Group' => 2);
$arrCondition[] = array('UID' => 'MainID', 'OverCredit' => 100000, 'OverStake' => 30000, 'Group' => 3);
$arrCondition[] = array('UID' => 'StockID', 'OverCredit' => 100000, 'OverStake' => 30000, 'Group' => 4);
$isCheckReport = true;

//=====================================================================================//
//找出有帳務使用者
foreach($arrCondition as $arrC){
	$strUID = $arrC["UID"];
	$intOverCredit = $arrC["OverCredit"];
	$intOverStake = $arrC["OverStake"];
	$intGroup = $arrC["Group"];
	$strError .= "Group => ".$intGroup.",  OverCredit => ".$intOverCredit.",  OverStake => ".$intOverStake."\n";

	$arrUID = array();
	$arrReport = array();
	if($isCheckReport){	//若是要檢查有帳務者
		$strSQL = "select `%s` as UID, SUM(`MemStake`) as tolStake from `bet_BetList` where PayupDate >= '%s' group by UID having tolStake >= %d";
		$strSQL = sprintf($strSQL, $strUID, $strPayupDate, $intOverStake);
		//print $strSQL;
		if(!$RS = PMA_mysql_query($strSQL, $$_Conn)){
			$GLOBALS["errormessage"] = "Find Wager Data Error!";
			$GLOBALS["errorhidden"] = "SQL: ".$strSQL.", \nError: ".PMA_mysql_error($$_Conn).", \nLine: ".__LINE__.", \nPage: ".__FILE__;
			$strError .= $GLOBALS["errormessage"]."<br>\n";
			$strError .= $GLOBALS["errorhidden"]."<br>\n";
		}

		while($Rows = PMA_mysql_fetch_object($RS)){
			$arrUID[] = $Rows -> UID;
		}
	}
	//exit();
	$strSQL = "update `bet_Member` set `SpGroup` = 0 where MemberGroup = ".$intGroup;
	if(!$RS = PMA_mysql_query($strSQL, $_ConnMaster)){
		$GLOBALS["errormessage"] = "Find Wager Data Error!";
		$GLOBALS["errorhidden"] = "SQL: ".$strSQL.", \nError: ".PMA_mysql_error($_ConnMaster).", \nLine: ".__LINE__.", \nPage: ".__FILE__;
		$strError .= $GLOBALS["errormessage"]."<br>\n";
		$strError .= $GLOBALS["errorhidden"]."<br>\n";
	}

	if(sizeof($arrUID) > 0){
		$strUID = implode(",", $arrUID);
		$strSQL = "update `bet_Member` set `SpGroup` = 1 where `ID` in (".$strUID.") and `isLock` = 0 and `CreditLine` >= ".$intOverCredit;
		if(!$RS = PMA_mysql_query($strSQL, $_ConnMaster)){
			$GLOBALS["errormessage"] = "Find Wager Data Error!";
			$GLOBALS["errorhidden"] = "SQL: ".$strSQL.", \nError: ".PMA_mysql_error($_ConnMaster).", \nLine: ".__LINE__.", \nPage: ".__FILE__;
			$strError .= $GLOBALS["errormessage"]."<br>\n";
			$strError .= $GLOBALS["errorhidden"]."<br>\n";
		}
		$strError .= "Affect Rows = ".mysql_affected_rows($_ConnMaster)."<br>\n";
	}
}
print $strError."\nEnd\n";
?>